;define(function (require) {
	'use strict';
	var $=require('jquery'),
		layer=require('layer'),
		echarts=require('echarts'),
		config=require('config'),
		$Fun=require('functions');
	
	function app(){}
	app.prototype={
		//map-table scripts
		init:function(){
			this.loadIndex = layer.load(2);
			
			//十六进制转rgba
			String.prototype.color2Rgba=function(){var sColor=this.toLowerCase(),reg=/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;if(sColor&&reg.test(sColor)){if(sColor.length===4){var sColorNew="#";for(var i=1;i<4;i+=1){sColorNew+=sColor.slice(i,i+1).concat(sColor.slice(i,i+1))}sColor=sColorNew}var sColorChange=[];for(var i=1;i<7;i+=2){sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)))}return"RGBA("+sColorChange.join(",")+","}else{return sColor}};
		
			var that = this,
				$mapWrap = $('#newMap');
			this.pageSize = this.getPageSize();
			
			//动画绑定
			$("[anim]").each(function(i,e){
				that.addAnimate({
					obj:$(e)
				});
			})
			
			var name = '南通市',pinyin = 'nantongshi';
			$Fun.getJsonData(config.mapConfig.urlBasePath+config.map2code[name], function(returnJson){
				echarts.registerMap(pinyin, returnJson);
				var $mapWrap = $('#newMap');
				if($mapWrap && $mapWrap.length>0){
					var mapChart = echarts.init($mapWrap[0]);
					var jsons = {
						"Data":{
							"areaData":
								[{"name":"启东市","value":68,"fwy":22,"gy":46},
								{"name":"海门市","value":60,"fwy":23,"gy":37},
								{"name":"开发区","value":43,"fwy":17,"gy":26},
								{"name":"崇川区","value":41,"fwy":32,"gy":9},
								{"name":"港闸区","value":38,"fwy":28,"gy":10},
								{"name":"通州区","value":63,"fwy":19,"gy":44},
								{"name":"如东县","value":65,"fwy":24,"gy":41},
								{"name":"如皋市","value":74,"fwy":30,"gy":44},
								{"name":"海安县","value":82,"fwy":25,"gy":57}],
							"colors":["#c65152"],
							"map":pinyin,
							"mapName":name,
							"max":82,
							"min":38},
						"Success":true
					};
					$Fun.drawChartMap( '', jsons.Data, function(rtn){
						//更新数据
						var option = rtn;
						option.series[0].zoom = 1.2;
						//that.optionTooltipCallback(option);
						mapChart.setOption(option);
						
						//bind mapchart click event
						mapChart.on('click',function(params){
							var $baidu = $('.baidu');
							if($baidu && $baidu.length>0){
								location.href='baidumap.html';
								return false;
							}
							//show table
							that.showTable(params);
						})
						
						that.showTable({name:'南通市'});
					})
				}
			})
			
			setTimeout(function(){
				layer.close(that.loadIndex);
				$('#loadBg').remove();
			}, 500);
		}
		
		,showTable:function(params){
			var that = this;
			var html = '';
			html += '<table>'+
				'<thead>'+
					'<tr>'+
						'<th class="TOTALYEAR" rowspan="2">时间</th>'+
						'<th colspan="6" class="indexName">'+params.name+'</th>'+
					'</tr>'+
					'<tr>'+
						'<th>增加值</th>'+
						'<th>增幅（%）</th>'+
					'</tr>'+
				'</thead>'+
				'<tbody>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.12</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.11</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.10</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.09</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.08</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.07</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.06</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.05</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.04</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.03</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.02</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
					'<tr>'+
						'<td class="TOTALYEAR">2015.01</td>'+
						'<td>'+that.getRandomNum(1,50,0)+'</td>'+
						'<td>'+that.getRandomNum(1,2,2)+'</td>'+
					'</tr>'+
				'</tbody>'+
			'</table>';
			$('#pie-simple').empty().append(html)
		}
		
		
		//动画生成
		,addAnimate:function(params){
			var that = this,
			 	$obj = params.obj,
			 	po = eval('('+$obj.attr('anim')+')'),
				zidx = po.zidx,                         //z-index
				name = po.name,                         //animation-name
				delay = po.delay,                       //animation-delay
				duration = po.duration,                 //animation-duration
				fun = po.fun?po.fun:'linear',       //animation-timing-function
				count = po.count?po.count:1;        //animation-iteration-count
			//取出原先style
			var _styles = $obj.attr("append-style");
			if(_styles){
				_styles = $obj.clone().attr("append-style");
			}else{
				_styles = '';
			}
			if(($obj.attr('stop') && $obj.attr('stop')=="false") || (typeof $obj.attr('stop') == 'undefined')){
				$obj.removeAttr("style");
				//重新动画
				setTimeout(function(){
					$obj.stop().addClass('animated').attr('style','animation-name:'+name+';visibility:visible;animation-delay:'+delay+'s;animation-duration:'+duration+'s;z-index:'+zidx+';animation-timing-function:'+fun+';animation-iteration-count:'+count+';'+_styles);
				}, 1);
			}else{
				$obj.stop().attr("stop",false);
			}
		}
		
		
		,getPageSize:function(){var xScroll,yScroll;if(window.innerHeight&&window.scrollMaxY){xScroll=window.innerWidth+window.scrollMaxX;yScroll=window.innerHeight+window.scrollMaxY}else{if(document.body.scrollHeight>document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight}else{xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight}}var windowWidth,windowHeight;if(self.innerHeight){if(document.documentElement.clientWidth){windowWidth=document.documentElement.clientWidth}else{windowWidth=self.innerWidth}windowHeight=self.innerHeight}else{if(document.documentElement&&document.documentElement.clientHeight){windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight}else{if(document.body){windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight}}}if(yScroll<windowHeight){var pageHeight=windowHeight}else{var pageHeight=yScroll}if(xScroll<windowWidth){var pageWidth=xScroll}else{var pageWidth=windowWidth}var arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize}
		
		,getRandomNum:function(a,b,n){
			n =  (typeof(n) == 'undefined') ? 2 : n;
			return (a + Math.random()*(b-a) ).toFixed(n);
		}
	}
	return app;
})

